<?php
class CMRPageShowSearch extends CMRPage
{
	private $keywords = array();
	
	public function __construct($database)
	{
		parent::__construct($database);
		$this->keywords = explode(' ',$_POST['searchText']);
//		print_r ($this->keywords);
	}
	
	protected function getResources()
	{
		$resources = array();
		
		$searchClause = "";
		foreach($this->keywords as $keyword)
		{
			$searchClause .= "(ResourceTitle LIKE '%".$keyword."%') OR (ResourceDescription LIKE '%".$keyword."%') OR (ResourceKeywords LIKE '%".$keyword."%') OR ";
//			$searchClause .= "(ResourceTitle LIKE 'B%') OR ";
		}
		$searchClause = substr($searchClause,0,-4);
//		echo $searchClause."<br>/n";
		
		
		$sql =  "SELECT * FROM Resources ";
		$sql .= "WHERE DeletedBy = '0' AND StatusTypeID = '3' AND (".$searchClause.") ";
		$sql .= "ORDER BY ResourceTitle";
//		echo $sql."<br>\n";
		$this->Database->ExecuteSQL($sql);
		while($row = $this->Database->getRow())
		{
			$resource = new Resource($this->Database->Recopy(),$row['ResourceID']);
			$resource->setCurrentUserGroup($this->userGroup);
			//			$resource->loadData($row['ResourceID'],$row['ResourceTitle'],$row['ResourceDescription'],$row['ResourceTypeID'],"","",,,,);
			array_push($resources,$resource);
		}
				
		return $resources;
	}
	
	protected function getContentsDiv()
	{
		$category = new Hidden();
		$category->setName("ResourceID");
		$html = $category->getHTML();
	
		$html .= "<div id=\"content\">\n";
		$html .= "<h1>Search</h1>\n";
		
/*		$html .= "<p style=\"text-align: center;\">\n";
		
		$html .= "<img id=\"ImgChoral\" width=\"130\" height=\"80\" ";
		if($this->section!='Choral')
		{
			$html .= "onMouseOver=\"OnMouseOverChoral();\" ";
			$html .= "onMouseOut=\"OnMouseOutChoral();\" ";
		}
		$html .= "onclick=\"OnClickChoral();\" ";
		$html .= "src=\"../Images/Choral";
		if($this->section!='Choral')$html .= 'A';
		$html .= ".png\" />&nbsp;&nbsp;&nbsp;\n";
		
		$html .= "<img id=\"ImgOrgan\" width=\"130\" height=\"80\" ";
		if($this->section!='Organ')
		{
			$html .= "onMouseOver=\"OnMouseOverOrgan();\" ";
			$html .= "onMouseOut=\"OnMouseOutOrgan();\" ";
		}
		$html .= "onclick=\"OnClickOrgan();\" ";
		$html .= "src=\"../Images/Organ";
		if($this->section!='Organ')$html .= 'A';
		$html .= ".png\" />&nbsp;&nbsp;&nbsp;\n";
		
		$html .= "<img id=\"ImgLiturgy\" width=\"130\" height=\"80\" ";
		if($this->section!='Liturgy')
		{
			$html .= "onMouseOver=\"OnMouseOverLiturgy();\" ";
			$html .= "onMouseOut=\"OnMouseOutLiturgy();\" ";
		}
		$html .= "onclick=\"OnClickLiturgy();\" ";
		$html .= "src=\"../Images/Liturgy";
		if($this->section!='Liturgy')$html .= 'A';
		$html .= ".png\" />&nbsp;&nbsp;&nbsp;\n";
		
		$html .= "<img id=\"ImgProfessional\" width=\"130\" height=\"80\" ";
		if($this->section!='Professional')
		{
			$html .= "onMouseOver=\"OnMouseOverProfessional();\" ";
			$html .= "onMouseOut=\"OnMouseOutProfessional();\" ";
		}
		$html .= "onclick=\"OnClickProfessional();\" ";
		$html .= "src=\"../Images/Professional";
		if($this->section!='Professional')$html .= 'A';
		$html .= ".png\" />&nbsp;&nbsp;&nbsp;\n";
		
		$html .= "</p>\n";
		$html .= "<p> This section contains the resources contained in the ".$this->categoryName." category of the ".$this->section." section. Choose a resource from the list below.</p>\n";
*/
		
		
		$resources = $this->getResources();
		if(count($resources)==0)$html .= "<p>There are currently no resources in this section</p>\n";
		foreach ($resources as $resource)
		{
			$html .= $resource->getSummaryHTML();
		}
				
		$html .= "</div>\n";
		return $html;
	}
	
}